package com.cmc.cloud.cmclink.doc.service.impl.so;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.SoSplitCombineDetailErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SoSplitCombineDetailConvert;
import com.cmc.cloud.cmclink.doc.entity.SoSplitCombineDetailDO;
import com.cmc.cloud.cmclink.doc.mapper.SoSplitCombineDetailMapper;
import com.cmc.cloud.cmclink.doc.service.so.SoSplitCombineDetailService;
import com.cmc.cloud.cmclink.doc.vo.sovo.sosplitcombinevo.SoSplitCombineDetailCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.sosplitcombinevo.SoSplitCombineDetailPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.sosplitcombinevo.SoSplitCombineDetailRespVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.sosplitcombinevo.SoSplitCombineDetailUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;


/**
 * 订舱拆分合并详细记录 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class SoSplitCombineDetailServiceImpl implements SoSplitCombineDetailService {

    @Resource
    private SoSplitCombineDetailMapper soSplitCombineDetailMapper;

    @Override
    public void createSoSplitCombineDetail(SoSplitCombineDetailCreateReqVO createReqVO) {
        // 插入
        SoSplitCombineDetailDO soSplitCombineDetail = SoSplitCombineDetailConvert.INSTANCE.convert(createReqVO);
        soSplitCombineDetailMapper.insert(soSplitCombineDetail);
    }

    @Override
    public void updateSoSplitCombineDetail(SoSplitCombineDetailUpdateReqVO updateReqVO) {
        // 校验存在
        validateSoSplitCombineDetailExists(updateReqVO.getId());
        // 更新
        SoSplitCombineDetailDO updateObj = SoSplitCombineDetailConvert.INSTANCE.convert(updateReqVO);
        soSplitCombineDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteSoSplitCombineDetail(Long id) {
        // 校验存在
        validateSoSplitCombineDetailExists(id);
        // 删除
        soSplitCombineDetailMapper.deleteById(id);
    }

    private void validateSoSplitCombineDetailExists(Long id) {
        if (soSplitCombineDetailMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(SoSplitCombineDetailErrorCodeConstants.SO_SPLIT_COMBINE_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public SoSplitCombineDetailDO getSoSplitCombineDetail(Long id) {
        return soSplitCombineDetailMapper.selectById(id);
    }

    @Override
    public List<SoSplitCombineDetailDO> getSoSplitCombineDetailList(Collection<Long> ids) {
        return soSplitCombineDetailMapper.selectBatchIds(ids);
    }

    @Override
    public List<SoSplitCombineDetailRespVO> getDetailListByOperationId(Long operationId) {
        LambdaQueryWrapperX<SoSplitCombineDetailDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(SoSplitCombineDetailDO::getOperationId, operationId);
        queryWrapper.orderByAsc(SoSplitCombineDetailDO::getId);
        return SoSplitCombineDetailConvert.INSTANCE.convertList(soSplitCombineDetailMapper.selectList(queryWrapper));
    }

    @Override
    public PageResult<SoSplitCombineDetailDO> getSoSplitCombineDetailPage(SoSplitCombineDetailPageReqVO reqVO) {
        LambdaQueryWrapperX<SoSplitCombineDetailDO> queryWrapper = new LambdaQueryWrapperX<>();
        return soSplitCombineDetailMapper.selectPage(reqVO, queryWrapper);
    }
}
